/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseItemsDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseItemsDao : BaseDao
    {
        private string defaultorder;
        private static BaseItemsDao instance = null;
        private static object locker = new object();

        public BaseItemsDao()
        {
            this.defaultorder = string.Empty;
            base.CurrentTableName = BaseItemsTable.TableName;
        }

        public BaseItemsDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseItemsDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseItemsDao(string tableName)
        {
            this.defaultorder = string.Empty;
            base.CurrentTableName = tableName;
        }

        public BaseItemsDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public BaseItemsDao(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName)
            : this(dbHelper, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        public string Add(BaseItemsEntity itemsEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseItemsTable.FieldParentID, itemsEntity.ParentID, BaseItemsTable.FieldCode, itemsEntity.Code))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return str;
            }
            if (this.Exists(BaseItemsTable.FieldParentID, itemsEntity.ParentID, BaseItemsTable.FieldFullName, itemsEntity.FullName))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            str = this.AddEntity(itemsEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseItemsEntity itemsEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseItemsTable.TableName);
            itemsEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(base.CurrentTableName);
            sqlBuilder.SetValue(BaseItemsTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, itemsEntity);
            sqlBuilder.SetValue(BaseItemsTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseItemsTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public override int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseItemsEntity itemsEntity = new BaseItemsEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseItemsTable.FieldID, DataRowVersion.Original].ToString();
                    if ((id.Length > 0) && base.AllowDelete)
                    {
                        num += this.Delete(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && (row[BaseItemsTable.FieldID, DataRowVersion.Original].ToString().Length > 0))
                {
                    itemsEntity.GetFrom(row);
                    if (base.AllowEdit)
                    {
                        num += this.UpdateEntity(itemsEntity);
                    }
                    else
                    {
                        num += this.SetProperty(base.ID, BaseItemsTable.FieldSortCode, base.SortCode);
                    }
                }
                if (row.RowState == DataRowState.Added)
                {
                    this.GetFrom(row);
                    num += (this.AddEntity(itemsEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseItemsEntity itemsEntity)
        {
            sqlBuilder.SetValue(BaseItemsTable.FieldParentID, itemsEntity.ParentID);
            sqlBuilder.SetValue(BaseItemsTable.FieldCode, itemsEntity.Code);
            sqlBuilder.SetValue(BaseItemsTable.FieldFullName, itemsEntity.FullName);
            sqlBuilder.SetValue(BaseItemsTable.FieldTargetTable, itemsEntity.TargetTable);
            sqlBuilder.SetValue(BaseItemsTable.FieldIsTree, itemsEntity.IsTree ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldUseItemCode, itemsEntity.UseItemCode ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldUseItemName, itemsEntity.UseItemName ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldUseItemValue, itemsEntity.UseItemValue ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldEnabled, itemsEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldAllowEdit, itemsEntity.AllowEdit ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldAllowDelete, itemsEntity.AllowDelete ? 1 : 0);
            sqlBuilder.SetValue(BaseItemsTable.FieldSortCode, itemsEntity.SortCode);
            sqlBuilder.SetValue(BaseItemsTable.FieldDescription, itemsEntity.Description);
        }

        public int Update(BaseItemsEntity itemsEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, base.CurrentTableName, itemsEntity.ID, itemsEntity.ModifyUserID, itemsEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseItemsTable.FieldParentID, itemsEntity.ParentID, BaseItemsTable.FieldCode, itemsEntity.Code, itemsEntity.ID))
            {
                statusCode = StatusCode.ErrorCodeExist.ToString();
                return num;
            }
            if (this.Exists(BaseItemsTable.FieldParentID, itemsEntity.ParentID, BaseItemsTable.FieldFullName, itemsEntity.FullName, itemsEntity.ID))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            num = this.UpdateEntity(itemsEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseItemsEntity itemsEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(base.CurrentTableName);
            this.SetEntity(sqlBuilder, itemsEntity);
            sqlBuilder.SetValue(BaseItemsTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseItemsTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseItemsTable.FieldID, itemsEntity.ID);
            return sqlBuilder.EndUpdate();
        }

        public string DefaultOrder
        {
            get
            {
                if (this.defaultorder.Length == 0)
                {
                    this.defaultorder = BaseItemsTable.FieldSortCode;
                }
                return this.defaultorder;
            }
            set
            {
                this.defaultorder = value;
            }
        }

        public static BaseItemsDao Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseItemsDao();
                        }
                    }
                }
                return instance;
            }
        }
    }
}